package com.wang.tasker.plug.net.email;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.WebCredentials;

import com.wang.tasker.core.ConfigElement;
import com.wang.tasker.core.ElementList;
import com.wang.tasker.core.Task;

public class EmailTask extends Task {
	
	private String to;
	private String cc;
	private String bcc;
	
	private String subject;
	private String body;
	private List attachmentList = new ArrayList();
	
	private String EWSUrl;
	private String emailAccount;
	private String password;

	@Override
	public void doWork() {
		ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
		ExchangeCredentials credentials = new WebCredentials(emailAccount,password);
		service.setCredentials(credentials);
		
			try {
				service.setUrl(new URI(EWSUrl));
			} catch (Exception e) {
				e.printStackTrace();
			}

        EmailMessage msg;
        try {
            msg = new EmailMessage(service);
            msg.setSubject(subject);
            msg.setBody(MessageBody
                    .getMessageBodyFromText(body));
            
            for(Object obj:attachmentList){
            	String f = (String)obj;
            	msg.getAttachments().addFileAttachment(f);
            }
            
            if(to != null){
            	String[] toList = to.split(";");
            	for(String toAdd:toList){
            		msg.getToRecipients().add(toAdd);
            	}
            }

            if(cc != null){
            	String[] ccList = cc.split(";");
            	for(String ccAdd:ccList){
            		msg.getCcRecipients().add(ccAdd);
            	}
            }
            
            if(bcc != null){
            	String[] bccList = bcc.split(";");
            	for(String bccAdd:bccList){
            		msg.getBccRecipients().add(bccAdd);
            	}
            }

            msg.send();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}

	@Override
	public void load(ConfigElement taskEle) {
		this.EWSUrl = taskEle.selectSingleElement("Server/EWSUrl").getTextTrim();
		this.emailAccount = taskEle.selectSingleElement("Server/EmailAccount").getTextTrim();
		this.password = taskEle.selectSingleElement("Server/Password").getTextTrim();
		
		ConfigElement toEle = taskEle.selectSingleElement("Message/TO");
		if(toEle != null)
			this.to = toEle.getTextTrim();
		
		ConfigElement ccEle = taskEle.selectSingleElement("Message/CC");
		if(ccEle != null)
			this.cc = ccEle.getTextTrim();
		
		ConfigElement bccEle = taskEle.selectSingleElement("Message/BCC");
		if(bccEle != null)
		this.bcc = bccEle.getTextTrim();
		this.subject = taskEle.selectSingleElement("Message/Subject").getTextTrim();
		this.body = taskEle.selectSingleElement("Message/Body").getTextTrim();
		
		ElementList list = taskEle.selectElements("Message/Attachments/Attachment");
		for(Object obj:list){
			ConfigElement ele = (ConfigElement)obj;
			//TODO check the file is exist. and save the absolutely path;
			/*
			 * 1.in package home
			 * 2.in file system
			 */
			this.attachmentList.add(ele.getTextTrim());
		}

	}

}
